package com.tyj.movie.controller;

import com.tyj.movie.config.security.UserDetailsPacking;
import com.tyj.movie.entity.Users;
import com.tyj.movie.mapper.UsersMapper;
import com.tyj.movie.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 日期: 2021/6/27 22:05
 * 作者: 谭友杰
 * 类说明:
 */
@Slf4j
@Controller
public class HelloController {

    @Autowired
    UsersMapper usersMapper;

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        return "8080服务器正在为您服务";
    }

    @RequestMapping("/moviecinema.html")
    public String test() {
        System.out.println("8081服务器正在为您服务");
        return "moviecinema";
    }

//    @RequestMapping("/t1")
//    public String test1(){
//        return "/error/loginError";
//    }


    @RequestMapping("redisTest")
    @ResponseBody
    public Object testRedis(HttpSession session) {
        Users users = usersMapper.selectById("1409711515617484801");
        session.setAttribute("user", users);
        Users user = (Users) session.getAttribute("user");
        return user;
    }

    @RequestMapping("securityRedis")
    @ResponseBody
    public Users securityRedis(HttpServletRequest request) {
//        //获得session对象
//        HttpSession session = request.getSession();
//        //取出session域中所有属性名
//        Enumeration attributeNames = session.getAttributeNames();
//        while (attributeNames.hasMoreElements()) {
//            System.out.println(attributeNames.nextElement());
//        }
//        //SPRING_SECURITY_CONTEXT
//        Object spring_security_context = session.getAttribute("SPRING_SECURITY_CONTEXT");
//        System.out.println(spring_security_context);
//        SecurityContext securityContext = (SecurityContext) spring_security_context;
//        //获得认证信息
//        Authentication authentication = securityContext.getAuthentication();
//        //获得用户详情
//        Object principal = authentication.getPrincipal();
//        Users user = (Users) principal;
//        String username = user.getUsername();
//        System.out.println(username);

        //第二种方法
        //获取上下栈
        UserDetailsPacking userDetailsPacking = (UserDetailsPacking) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        Users user1 = userDetailsPacking.getUsers();
        return user1;
    }

    @RequestMapping("editRedisSession")
    @ResponseBody
    public Object editRedisSession(HttpServletRequest request) {
        //1.从HttpServletRequest中获取SecurityContextImpl对象
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");

        //2.从SecurityContextImpl中获取Authentication对象
        Authentication authentication = securityContextImpl.getAuthentication();
        UserDetailsPacking userDetailsPacking= (UserDetailsPacking) authentication.getPrincipal();
        userDetailsPacking.getUsers().setNickname("123");

        //3.初始化UsernamePasswordAuthenticationToken实例 ，这里的参数user就是我们要更新的用户信息
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(userDetailsPacking, authentication.getCredentials());
        auth.setDetails(authentication.getDetails());

        //4.重新设置SecurityContextImpl对象的Authentication
        securityContextImpl.setAuthentication(auth);

        Users sessionObject = SecurityUtil.getSessionUser();
        return sessionObject;
    }


}
